//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef FLSYSTEM_H
#define FLSYSTEM_H

//#include "excustom.h"
#include "flcustom.h"
#include <string.h>
//#include <stdlib.h>
//#include <stdio.h>
#include <assert.h>

//#define	NULL	(void *)0

/*
 * Yielding the CPU
 *
 * TrueFFS utilizes the routine flSleep to yield the CPU while
 * waiting for time consuming operations like a flash erase.
 * If the routine is not implemented, then uncomment the define below.
 */

#define DO_NOT_YIELD_CPU

/* Little-endian/big-endian
 *
 * FAT and translation layer structures use the little-endian (Intel)
 * format for integers.
 * If your machine uses the big-endian (Motorola) format, uncomment the
 * following line.
 * Note that even on big-endian machines you may omit the BIG_ENDIAN
 * definition for smaller code size and better performance, but your media
 * will not be compatible with standard FAT and FTL.
 */

/* #define FL_BIG_ENDIAN */

/* Far pointers
 *
 * Specify here which pointers may be far, if any.
 * Far pointers are usually relevant only to 80x86 architectures.
 *
 * Specify FAR_LEVEL:
 *   0 - If using a flat memory model or having no far pointers.
 *   1 - If only the socket window may be far
 *   2 - If only the socket window and caller's read/write buffers
 *       may be far.
 *   3 - If socket window, caller's read/write buffers and the
 *       caller's I/O request packet may be far.
 */

//#define FAR_LEVEL   2//            0
#define FAR_LEVEL     0

/* Pointer arithmetic
 *
 * The following macros define machine- and compiler-dependent macros for
 * handling pointers to physical window addresses. The definitions below are
 * for PC real-mode Borland-C.
 *
 * 'physicalToPointer' translates a physical flat address to a (far) pointer.
 * Note that if your processor uses virtual memory, the code should
 * map the physical address to virtual memory, and return a pointer to that
 * memory (the size parameter tells how much memory should be mapped).
 *
 * 'addToFarPointer' increments to a pointer and returns a new
 * pointer. The increment may be as large as your window size. The code
 * below assumes that the increment may be larger than 64 KB and so performs
 * huge pointer arithmetic.
 */

#define FL_IOCTL_START	0xffff0000

#if FAR_LEVEL > 0
#include <dos.h>

#define physicalToPointer(physical,size,drive)          \
	MK_FP( ((int)((physical) >> 4)) , ((int) (physical) & 0xF) )

#define pointerToPhysical(ptr)                  \
	(((unsigned long) FP_SEG(ptr) << 4) + FP_OFF(ptr))

#define addToFarPointer(base,increment)                  \
        MK_FP((FP_SEG(base) +                            \
		((unsigned short) ((FP_OFF(base) + (increment)) >> 16) << 12)), \
	      (FP_OFF(base) + (int) (increment)))

#define freePointer(ptr,size)

#else /* FAR_LEVEL > 0 */

extern unsigned long doc_VirtBase;
#define physicalToPointer(physical,size,drive)          \
	((void *) ((unsigned long)(physical) + doc_VirtBase))

#define pointerToPhysical(ptr)  ((unsigned long)(ptr))

#define addToFarPointer(base,increment)         \
	((void *) ((unsigned char *) (base) + (increment)))

#define freePointer(ptr,size)

#endif /* FAR_LEVEL > 0 */

/*
#define physicalToPointer(physical,size,drive)    ((void *) (physical))

#define pointerToPhysical(ptr)  ((unsigned long)(ptr))

#define addToFarPointer(base,increment) ((void *) ((unsigned char *) (base) + (increment)))

#define freePointer(ptr,size)

*/
/* Default calling convention
 *
 * C compilers usually use the C calling convention to routines (cdecl), but
 * often can also use the pascal calling convention, which is somewhat more
 * economical in code size. Some compilers also have specialized calling
 * conventions which may be suitable. Use compiler switches or insert a
 * #pragma here to select your favorite calling convention.
 */

/* Mutex type
 *
 * If you intend to access the TrueFFS API in a multi-tasking environment,
 * you may need to implement some resource management and mutual-exclusion
 * of TrueFFS with mutex & semaphore services that are available to you. In
 * this case, define here the Mutex type you will use, and provide your own
 * implementation of the Mutex functions in flcustom.c
 *
 * By default, a Mutex is defined as a simple counter, and the Mutex
 * functions in flcustom.c implement locking and unlocking by incrementing
 * and decrementing the counter. This will work well on all single-tasking
 * environments, as well as on many multi-tasking environments.
 */

typedef int FLMutex;

/* Memory allocation
 *
 * The translation layers (e.g. FTL, NFTL, INFTL) need to allocate memory to
 * handle the Flash media. The required size depends on the media being handled.
 *
 * You may choose to use the standard 'malloc' and 'free' to handle such
 * memory allocations, provide your own equivalent routines, or you may
 * choose not to define any memory allocation routine. In this case, the
 * memory will be allocated statically at compile-time on the assumption of
 * the largest media configuration you need to support. This is the simplest
 * choice, but may cause your RAM requirements to be larger than you
 * actually need.
 *
 * If you define routines other than malloc & free, they should have the
 * same parameters and return types as malloc & free. You should either code
 * these routines in flcustom.c or include them when you link your application.
 */


/* #define MALLOC malloc */
/* #define FREE   free */


/* Debug mode
 *
 * Uncomment the following lines if you want debug messages to be printed
 * out. Messages will be printed at initialization key points, and when
 * low-level errors occure.
 * You may choose to use 'printf' or provide your own routine.
 */

#define DEBUG_PRINT(str)  /*printk(str)*/
/* #define LOG_FILE */ /* log edc errors                              */

/* Special debug massages for dformat utility */
#define DFORMAT_PRINT(str)  /*printk(str)*/
/* File open macro */
#define FL_FOPEN      /* fopen */
/* File close macro */
#define FL_FCLOSE     /* fclose */
/* File printf macro */
#define FL_FPRINTF    /* fprintf */

/*************************************/
/* TrueFFS 6.0 debug print mechanism */
/*************************************/
#define FLZONE_NONE     0
#define FLZONE_MTD      0x0001
#define FLZONE_BDK      0x0002
#define FLZONE_TL       0x0004
#define FLZONE_BLKDEV   0x0008
#define FLZONE_FORMAT   0x0010
#define FLZONE_FULL     0xffff

/*extern void PrintMsg(const char *fmt,...);*/

#define DBG_PRINT_FLOW(zone,str)   {if (zone == 0x10) printk(str);}
#define DBG_PRINT_FLOW_PRM(zone,str)
#define DBG_PRINT_ERR(zone,str)    printk(str)
#define DBG_PRINT_ERR_PRM(zone,str)
#define DBG_PRINT_WRN(zone,str)    printk(str)
#define DBG_PRINT_WRN_PRM(zone,str)

#ifndef ENVIRONMENT_VARS

/* #define tffscpy flmemcpy */
/* #define tffscmp flmemcmp */
/* #define tffsset flmemset */

#define tffscpy MyMemCpy
#define tffscmp MyMemCmp
#define tffsset MyMemSet

#else   /* #ifndef ENVIRONMENT_VARS */


#define flcpy flmemcpy
#define flcmp flmemcmp
#define flset flmemset

#endif  /* #ifndef ENVIRONMENT_VARS */

 /* Remove runtime controll over memory access routines
 *
 * If defined memory access routines will be set at compile time using
 * dedicated defintions in flsystem.h
 * Note : when compile time customization is chosen, you must sepcify
 * the bus width even when working with DiskOnChip Millennium Plus.
 * Refer to Trueffs manual for more infromation.
 */

//#define FL_NO_USE_FUNC

/* Improoving performance by using native code.
 *
 * You can improove the way TrueFFS utilizes 32bit (or larger) native variables
 * by uncommenting the defintion bellow.
 */

/* #define FL_ASSUME_NATIVE_IS_32BITS */

/* System clean up routine
 *
 * This routine will be called every time TrueFFS exist.
 * The implementation is found in flsystem.c
 */
/*
extern void flSysfunExit(void);

#define FL_SYS_FUNC_RELEASE flSysfunExit()
*/

/* Control over the priority of the TrueFFS thread
 *
 * Some of TrueFFS time is spend on polling the flash ready\busy signal.
 * TrueFFS supplies a verity of ways to utilize this time, one of which is
 * to inform the OS that TrueFFS priority can be lowered for the time being.
 */

#define FL_LowerPriorityThread(flashPtr)
#define FL_RaisePriorityThread(flashPtr)


/* Removing backward compatibility basic type definitions.
 *
 * TrueFFS 5.x used the byte, word, dword, Sbyte, Sword and Sdword basic types.
 * Since these names are used by several major OSs, such as Windows and
 * VxWorks, it was decided to rename these types with less general names
 * (See the FLxxx types below). As many TrueFFS-based applications already
 * use these types, TrueFFS still automatically defines them. If these
 * definitions cause your compiler to report redefinition errors, simple
 * define FL_DISABLE_OLD_TRUEFFS_TYPES and TrueFFS will automatically remove
 * the definition. */

#define FL_DISABLE_OLD_TRUEFFS_TYPES


/* Optionally you can define TrueFFS basic types:
 *
 * FLByte, FLWord, FLDword, FLSByte, FLSWord, FLSDword,
 */


/* Optionally you can Define I/O memory access macros for accessing the
 * DiskOnChip registers ( only needed if the device is not memory mapped)
 *
 * FLWRITE_IO_BYTE        , FLREAD_IO_BYTE
 * FLWRITE_IO_WORD        , FLREAD_IO_WORD
 * FLWRITE_IO_DWORD       , FLREAD_IO_DWORD
 * TFFSCPY_FROM_IO_8_BITS , TFFSCPY_FROM_IO_16_BITS
 * TFFSCPY_TO_IO_8_BITS   , TFFSCPY_TO_IO_16_BITS
 * TFFSSET_IO_8_BITS      , TFFSSET_IO_16_BITS
 */

#endif /* FLSYSTEM_H */
